//
//  CollectView.m
//  项目三
//
//  Created by rimi on 15/10/9.
//  Copyright (c) 2015年 rimi. All rights reserved.
//  

#import "CollectView.h"
@interface CollectView ()

- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation CollectView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initializeUserInterface];
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initializeUserInterface];
    }
    return self;
}

- (void)initializeUserInterface {

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
    imageView.image = [UIImage imageNamed:@"书架"];
    [self addSubview:imageView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn setFrame:DHFlexibleFrame(CGRectMake(631, 7, 46, 38))];
    [btn addTarget:self action:@selector(respondsToPopBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    
    UIViewController *topVC = [self appRootViewController];
    self.frame = DHFlexibleFrame(CGRectMake(10, 1024, 681, 901));
    [topVC.view addSubview:self];
    
    self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
}
- (void)respondsToPopBtn:(UIButton *)sender {
    
    [self removeFromSuperview];
}

- (UIViewController *)appRootViewController {
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}

#pragma mark - system methods
- (void)willMoveToSuperview:(UIView *)newSuperview {
    if (newSuperview == nil) {
        return;
    }
    
    CGRect afterFrame = DHFlexibleFrame(CGRectMake(10, 0, 681, 901));
    
    [UIView animateWithDuration:0.25f delay:0.0 options: UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame = afterFrame;
        self.alpha = 1;
        
    } completion:^(BOOL finished) {
        
    }];
    [super willMoveToSuperview:newSuperview];
}

- (void)removeFromSuperview {
    CGRect afterFrame = CGRectMake(10, 944, 681, 901);
    
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame = afterFrame;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [super removeFromSuperview];
    }];
}
@end
